﻿

using System;
using System.Collections.Generic;
using System.Data;
using BusinessObjects.BussinessDictionary;
using DataObjects.Shared;
using DataObjects.StandartInterfaces.Dictionary;

namespace DataObjects.ADO.SqlServer.Dictionary
{

    /// <summary>
    /// 
    /// </summary>
    public class SqlServerDbOptionDao : IDbOptionDao
    {
        /// <summary>
        /// Takes the specified employ test.
        /// </summary>
        /// <param name="dbOption">The employ test.</param>
        /// <returns></returns>
        private static object[] Take(DbOption dbOption)
        {
            return new object[]  
            {
               "@OptionID", dbOption.OptionId,
                "@Code", dbOption.Code,
                "@Description", dbOption.Description,
                "@IsSystem", dbOption.IsSystem,
                "@Value", dbOption.Value,
                "@ValueType", dbOption.ValueType,
                "@UnitID", dbOption.UnitId
            };
        }
        /// <summary>
        /// The make
        /// </summary>
        private static readonly Func<IDataReader, DbOption> Make = reader =>
          new DbOption
          {
              OptionId = reader["OptionID"].AsId(),
              Code = reader["Code"].AsString(),
              Description = reader["Description"].AsString(),
              IsSystem = reader["IsSystem"].AsBool(),
              Value = reader["Value"].AsString(),
              ValueType = reader["ValueType"].AsInt(),
              UnitId = reader["UnitID"].AsInt()
          };

        /// <summary>
        /// Gets the paged.
        /// </summary>
        /// <param name="dbOptionId">The dbOption identifier.</param>
        /// <param name="currentPage">The current page.</param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public List<DbOption> GetPaged(int dbOptionId, int currentPage)
        {
            const string sql = @"uspGetPaged_DbOption";
            object[] parms = { "@WhereClause", null, "@OrderBy", null, "@PageIndex", currentPage - 1, "@DbOptionID", dbOptionId };
            return Db.ReadList(sql, CommandType.StoredProcedure, Make, parms);
        }

        /// <summary>
        /// </summary>
        /// <param name="dbOptionId"></param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public int GetPageSize(int dbOptionId)
        {
            const string sql = @"uspGetPageSize_DbOption";
            object[] parms = { "@WhereClause", null, "@DbOptionID", dbOptionId };
            return Db.GetCount(sql, CommandType.StoredProcedure, parms);
        }

       

       

        /// <summary>
        /// Inserts the dbOption.
        /// </summary>
        /// <param name="dbOption">The dbOption.</param>
        /// <returns></returns>
        public string InsertDbOption(DbOption dbOption)
        {
            const string sql = "uspInsert_DbOption";
            return Db.Insert(sql, CommandType.StoredProcedure, Take(dbOption));
        }

        /// <summary>
        /// Updates the dbOption.
        /// </summary>
        /// <param name="dbOption">The dbOption.</param>
        /// <returns></returns>
        public string UpdateDbOption(DbOption dbOption)
        {
            const string sql = "uspUpdate_DbOption";
            return Db.Update(sql, CommandType.StoredProcedure, Take(dbOption));
        }

        /// <summary>
        /// Deletes the dbOption.
        /// </summary>
        /// <param name="dbOption">The dbOption.</param>
        /// <returns></returns>
        public string DeleteDbOption(DbOption dbOption)
        {
            const string sql = @"uspDelete_DbOption";
            object[] parms = { "@EmployID", dbOption.OptionId };
            return Db.Delete(sql, CommandType.StoredProcedure, parms);
        }





        public DbOption GetDbOption(int dbOptionId)
        {
            const string sql = @"uspGet_DbOption_ByID";
            object[] parms = { "@EmployID", dbOptionId };
            return Db.Read(sql, CommandType.StoredProcedure,Make, parms);
        }


        public List<DbOption> GetDbOptionsByUnit(int unitId)
        {
            const string sql = @"uspGet_DbOption_ByUnitID";
            object[] parms = { "@UnitId", unitId };
            return Db.ReadList(sql, CommandType.StoredProcedure, Make, parms);
          
        }

        public List<DbOption> GetDbOptionsByUnit(int unitId, bool isActive)
        {
            const string sql = @"uspGet_DbOption_ByUnitAndActive";
            object[] parms = { "@WhereClause", " UnitID = '" + unitId + "' AND Inactive = '" + isActive + "'" };
            return Db.ReadList(sql, CommandType.StoredProcedure, Make, parms);
        }

        public string InsertDbOptionByUnit(int unitId)
        {
            const string sql = @"uspInsert_DbOption_ByUnit";
            object[] parms = { "@UnitID", unitId };
            return Db.Insert(sql, CommandType.StoredProcedure, parms);
        }
    }
}
